package com.manager.common.init;

import com.manager.common.job.IntervalBackupContainerVolume;
import com.manager.common.quartz.impl.CornJonEntity;
import com.manager.common.strategy.StrategyManager;
import com.manager.common.utils.DataTools;
import com.manager.common.utils.LargeFileHttpUploadUtils;
import com.manager.container.service.DockerDeployService;
import com.manager.model.DockerDeploy;
import com.manager.model.SchedulingStrategy;
import com.manager.model.System_User;
import com.manager.schedule.service.SchedulingService;
import com.manager.system.service.DictionaryDataService;
import com.manager.system.service.SystemSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SpringBootInit implements ApplicationRunner {

    @Value("${spring.web.resources.static-locations}")
    private String locationsDir;

    @Value("${web.root.userName}")
    String userName;

    @Value("${web.root.password}")
    String password;

    @Autowired
    DictionaryDataService dictionaryDataService;

    @Autowired
    SystemSettingService systemSettingService;

    @Autowired
    DockerDeployService dockerDeployService;

    @Autowired
    SchedulingService schedulingService;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        LargeFileHttpUploadUtils.setBasePath(locationsDir.replaceAll("file:", "") + "/");

        System_User systemUser = new System_User();
        systemUser.setUserName(userName);
        systemUser.setPassword(password);
        systemUser.setHasAdmin(1);
        systemUser.setHasOpen(1);

        DataTools.adminList.add(systemUser);
        dictionaryDataService.initDictonaryData();
        systemSettingService.initSystemSettingCache();

        List<SchedulingStrategy> schedulingStrategies = schedulingService.getSchedulingStrategies();
        schedulingStrategies.forEach(schedulingStrategy -> {
            StrategyManager.setSchedulingStrategy(schedulingStrategy.getId(),schedulingStrategy);
        });

        /*List<DockerDeploy> list = dockerDeployService.getDockerDeployBackupList();
        if(list != null && list.size() > 0){
            for(DockerDeploy dockerDeploy: list){
                String aliseName = "备份容器挂载数据" + "【"+dockerDeploy.getContainerName()+"】" + "【"+dockerDeploy.getContainerID()+"】";
                CornJonEntity cornJonEntity = new CornJonEntity(dockerDeploy.getId(),
                        aliseName, IntervalBackupContainerVolume.class, dockerDeploy,dockerDeploy.getScheduledCorn());
                cornJonEntity.start();
            }
        }*/
    }
}

